package com.example.basic_ui_demo.view.teams

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.example.basic_ui_demo.data_class.teams.Team
import com.example.basic_ui_demo.view_model.TeamsViewModel
import com.example.basic_ui_demo.view_model.TeamsViewModel.Companion.tabs
import com.lt.compose_views.pager_indicator.TextPagerIndicator

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun InformationCompose(viewModel: TeamsViewModel, team: Team) {
    LazyColumn(
        contentPadding = PaddingValues(top = 120.dp)
    ) {
        item {
            TextPagerIndicator(
                texts = tabs,
                offsetPercentWithSelectFlow = viewModel.composePagerState.createChildOffsetPercentFlow(),
                selectIndexFlow = viewModel.composePagerState.createCurrSelectIndexFlow(),
                fontSize = 16.sp,
                selectFontSize = 16.sp,
                textColor = Color.Black,
                selectTextColor = Color.Green,
                selectIndicatorColor = Color.Green,
                onIndicatorClick = { viewModel.composePagerState.setPageIndexWithAnimate(it) },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(30.dp),
                userCanScroll = true,
                margin = 20.dp,
            )
        }
        item {
            Row(
                Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .background(Color.LightGray),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.Center
            ) {
                Text(text = "基本信息")
            }
        }
        item { InformationRow(header = "俱乐部:", info = team.name) }
        item { InformationRow(header = "成立时间:", info = "${team.founded}年") }
        item { InformationRow(header = "所在地区:", info = team.area.name, picUrl = team.area.flag)}
        item { InformationRow(header = "主场:", info = team.venue) }
        item { InformationRow(header = "官网:", info = team.website) }
    }
}

@Composable
fun InformationRow(header: String, info: String) {
    Row(
        Modifier
            .fillMaxWidth()
            .height(80.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start
    ) {
        Text(text = header, color = Color.DarkGray, modifier = Modifier.padding(20.dp))
        Text(text = info, modifier = Modifier.padding(20.dp))
    }
    HorizontalDivider(color = Color.DarkGray)
}

@Composable
fun InformationRow(header: String, info: String, picUrl: String) {
    Row(
        Modifier
            .fillMaxWidth().height(80.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start
    ) {
        Text(text = header, color = Color.DarkGray, modifier = Modifier.padding(20.dp))
        Text(text = info, modifier = Modifier.padding(20.dp))
        Box(
            modifier = Modifier
                .weight(1f)
                .fillMaxHeight(0.8f)
                .padding(20.dp)
        ) {
            AsyncImage(
                model = picUrl, contentDescription = "",
                contentScale = ContentScale.FillHeight,
            )
        }
    }
    HorizontalDivider(color = Color.DarkGray)
}